package com.msmall.community.tag;

import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.msmall.base.util.StringUtil;
import com.msmall.community.user.model.UserProfile;

public class SelectFieldTag extends TagSupport {

	private static final long serialVersionUID = -131181397178917083L;

	private String type; // SALARY  CHARACTEREISE

	private String id;
	
	private String name;
	
	private String css;
	
	private String selected;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}
	
	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		StringBuffer buf = new StringBuffer();
		try{
			Map<String, String> map = null;
			if("SALARY".equalsIgnoreCase(type)){
				map = UserProfile.SALARY;
			}
			if("CHARACTERISE".equalsIgnoreCase(type)){
				map = UserProfile.CHARACTERISE;
			}
			if("BODYTYPE".equalsIgnoreCase(type)){
				map = UserProfile.BODYTYPE;
			}
			if("SMOKE".equalsIgnoreCase(type)){
				map = UserProfile.SMOKE;
			}
			if("DRINK".equalsIgnoreCase(type)){
				map = UserProfile.DRINK;
			}
			if("ASTERISM".equalsIgnoreCase(type)){
				map = UserProfile.ASTERISM;
			}
			if("BLOODTYPE".equalsIgnoreCase(type)){
				map = UserProfile.BLOODTYPE;
			}
			if("MARRIED".equalsIgnoreCase(type)){
				map = UserProfile.MARRIED;
			}
			if("EDUDEGREE".equalsIgnoreCase(type)){
				map = UserProfile.EDUDEGREE;
			}
			if("EDUSPECIALTY".equalsIgnoreCase(type)){
				map = UserProfile.EDUSPECIALTY;
			}
			if("JOBTITLE".equalsIgnoreCase(type)){
				map = UserProfile.JOBTITLE;
			}
			if("JOBPROFESSION".equalsIgnoreCase(type)){
				map = UserProfile.JOBPROFESSION;
			}
			if(map != null && !map.isEmpty()){
				buf.append("<select name=\"").append(name).append("\"");
				if(!StringUtil.nullOrBlank(id)){
					buf.append(" id=\"").append(id).append("\"");
				}
				if(!StringUtil.nullOrBlank(css)){
					buf.append(" class=\"").append(css).append("\"");
				}
				buf.append(">");
				for(Map.Entry<String, String> entry: map.entrySet()){
					String value = entry.getKey();
					String desc = entry.getValue();
					if(value.equals(selected)){
						buf.append(getOption(value, desc, true ));
					}else{
						buf.append(getOption(value, desc, false ));
					}
				}
				buf.append("</select>");
			}
			writer.print(buf.toString());
		}catch(Exception e){
			
		}
		return (SKIP_BODY);
	}
	
	private String getOption(String value, String message, boolean selected){
		StringBuffer buf = new StringBuffer();
		buf.append("<option value=\"").append(value).append("\"");
		if(selected){
			buf.append("selected=\"selected\"");
		}
		buf.append(">").append(message).append("</option>");
		return buf.toString();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}
}
